#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <linux/videodev2.h>
#include <sys/ioctl.h>

int set_control(int fd, int control_id, int value) {
    struct v4l2_control control;
    control.id = control_id;
    control.value = value;

    if (ioctl(fd, VIDIOC_S_CTRL, &control) == -1) {
        perror("Setting control failed");
        return -1;
    }
    return 0;
}

int main() {
    const char *device = "/dev/video0";  // 替换为您的 UVC 设备
    int fd = open(device, O_RDWR);
    if (fd == -1) {
        perror("Opening video device");
        return EXIT_FAILURE;
    }

    // 设置曝光
    if (set_control(fd, V4L2_CID_EXPOSURE, 100) == -1) {
        close(fd);
        return EXIT_FAILURE;
    }

    // 设置白平衡
    if (set_control(fd, V4L2_CID_WHITE_BALANCE_TEMPERATURE, 5000) == -1) {
        close(fd);
        return EXIT_FAILURE;
    }

    // 设置伽玛
    if (set_control(fd, V4L2_CID_GAMMA, 100) == -1) {
        close(fd);
        return EXIT_FAILURE;
    }

    // 设置亮度
    if (set_control(fd, V4L2_CID_BRIGHTNESS, 128) == -1) {
        close(fd);
        return EXIT_FAILURE;
    }
        // 设置对比度
    if (set_control(fd, V4L2_CID_CONTRAST, 128) == -1) {  // 128 是对比度的示例值
        close(fd);
        return EXIT_FAILURE;
    }

    printf("Controls set successfully.\n");
    close(fd);
    return EXIT_SUCCESS;
}


/*
    //ubuntu 系统上
    sudo apt-get install libv4l-dev

    gcc -o uvc_control uvc_control.c -lv4l2
    sudo ./uvc_control

    替换 V4L2_CID_EXPOSURE、V4L2_CID_WHITE_BALANCE_TEMPERATURE、V4L2_CID_GAMMA 和 V4L2_CID_BRIGHTNESS 的值为适合您设备的控制 ID。
    可以使用 v4l2-ctl --list-ctrls 命令查看设备支持的控制项。


*/